package com.ruantu.clothing.controller.report;

import com.ruantu.clothing.controller.RukuController;
import com.ruantu.clothing.entity.JsonResponseMessage;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.SelectStoreResult;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.entity.report.Rukuyuebao;
import com.ruantu.clothing.service.report.RukuReportService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.List;

@RequestMapping("/report")
@Controller
public class RukuReportController {
    private static final Logger logger = LoggerFactory.getLogger(RukuController.class);
    @Autowired
    private RukuReportService rukuReportService;

    /**
     * 入库月表
     *
     * @param monSearch
     * @param selectText
     * @param //session
     * @return
     */
    @RequestMapping("/gtRukuyuebao")
    @ResponseBody
    public JsonResponseMessage gtRukuyuebao(String monSearch, String selectText, String jgid) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(rukuReportService.gtRukuyuebaoday(monSearch, selectText, jgid));
        return jsonResponseMessage;
    }

    /**
     * 入库报表
     *
     * @param stime
     * @param etime
     * @param selectText
     * @param pageBean
     * @param //session
     * @return
     */
    @RequestMapping("/gtrukureport")
    @ResponseBody
    public JsonResponseMessage gtrukureport(String stime, String etime, String selectText, String cpYear,String season,String pinpai,
                                            Integer ordTJ, PageBean pageBean, String jgid) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(rukuReportService.gtrukureport(stime, etime, jgid, selectText, cpYear,season,pinpai,ordTJ, pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        jsonResponseMessage.setFooter(rukuReportService.geAllTotalByTime(stime, etime,selectText,cpYear,season,pinpai,jgid));
        return jsonResponseMessage;
    }

    /**
     * 导出入库报表
     * @param stime
     * @param etime
     * @param selectText
     * @param ordTJ
     * @param jgid
     * @param out
     * @param response
     */
    @RequestMapping("/downloadExcelMuban")
    @ResponseBody
    public void daoChuMobanOrderUser(String stime, String etime, String selectText,String cpYear,String season,String pinpai, Integer ordTJ, String jgid,
                                     ServletOutputStream out, HttpServletResponse response) {
        try {
            XSSFWorkbook wb = rukuReportService.download(stime,etime,selectText,cpYear,season,pinpai,ordTJ,jgid);
            HttpHeaders headers = new HttpHeaders();
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            String fileName = URLEncoder.encode(stime+"---"+etime+"入库报表.xls", "utf-8");
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            wb.write(out);
            outByteStream.flush();
            outByteStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 导出入库月表
     * @param selectText
     * @param jgid
     * @param out
     * @param response
     */
    @RequestMapping("/gtRukuyuebaoExcel")
    @ResponseBody
    public void gtRukuyuebaoExcel(String monSearch, String selectText,String jgid,
                                     ServletOutputStream out, HttpServletResponse response) {
        try {
            XSSFWorkbook wb = rukuReportService.gtRukuyuebaoExcel(monSearch,selectText,jgid);
            HttpHeaders headers = new HttpHeaders();
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            String fileName = URLEncoder.encode("入库月表.xls", "utf-8");
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            wb.write(out);
            outByteStream.flush();
            outByteStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
